import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { ApiException } from '@main/common/exceptions/api.exception';
import SysErpField from '@main/entities/service/admin/sys/sys-erp-field.entity';
import { Like, Repository } from 'typeorm';
import { SysOperationLogService } from '../operationLog/operationLog.service';

@Injectable()
export class SysErpFieldService {
  constructor(
    @InjectRepository(SysErpField)
    private erpFieldRepository: Repository<SysErpField>,
    private operationLogService: SysOperationLogService,

  ) {}

  /**
   * 罗列所有配置
   */
  async page(data) {
    const { pageSize, pageNum, entity = {} } = data;

    const { field } = entity;

    return await this.erpFieldRepository.findAndCount({
      where: {
        field: Like(`%${field}%`),
      },
      order: {
        updateTime: 'DESC',
      },
      take: pageSize,
      skip: (pageNum - 1) * pageSize,
    });
  }

  /**
   * 新增
   */
  async add(data, user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];
    const erpField = await this.erpFieldRepository.insert({
      ...data,
      createName: user.nickName,
      createUserName: user.userName,
      updateName: user.nickName,
      updateUserName: user.userName,
    });
    databaseOperationLogList.push({
      type: 'add',
      dataType: 'obj',
      database: 'SysErpField',
      checkOnlyKeyList: [],
      data: erpField,
      whereObj: null,
      remark: `SysErpField表新增数据`,
    });
    await this.operationLogService.add(
      {
        type: 'add',
        msg: `erp字段转换模块新增字段`,
        databaseOperationLogList,
        remark: `新增字段${data.field}`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  /**
   * 更新
   */
  async update(data, user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];
    await this.erpFieldRepository.update(
      { id: data.id },
      {
        newField: data.newField,
        remark: data.remark,
        updateName: user.nickName,
        updateUserName: user.userName,
      }
    );
    databaseOperationLogList.push({
      type: 'update',
      dataType: 'obj',
      database: 'SysErpField',
      checkOnlyKeyList: [],
      data: {
        newField: data.newField,
        remark: data.remark,
        updateName: user.nickName,
        updateUserName: user.userName,
      },
      whereObj: { id: data.id },
      remark: `SysErpField表更新字段信息`,
    });
    await this.operationLogService.add(
      {
        type: 'update',
        msg: `erp字段转换模块更新字段信息`,
        databaseOperationLogList,
        remark: `更新字段${data.field}`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  /**
   * 删除
   */
  async delete(idList, user, mac, ip, syncHostSqlite) {
    const databaseOperationLogList = [];
    await this.erpFieldRepository.delete(idList);
    databaseOperationLogList.push({
      type: 'delete',
      dataType: 'array',
      database: 'SysErpField',
      checkOnlyKeyList: [],
      data: null,
      whereObj: idList,
      remark: `SysErpField表删除字段`,
    });
    await this.operationLogService.add(
      {
        type: 'delete',
        msg: `erp字段转换模块删除字段信息`,
        databaseOperationLogList,
        remark: `删除字段`,
        mac,
        ip,
        syncHostSqlite: isNaN(Number(syncHostSqlite)) ? 0 : Number(syncHostSqlite),
      },
      user
    );
  }

  async isExistKey(data: any) {
    const result = await this.erpFieldRepository.findOne({ where: { field: data.field } });

    if (result) {
      throw new ApiException(10047);
    }
  }
}
